package com.dragon.array;

/**
 * @author dragon
 * @version V1.0.0-RELEASE
 * @date 2019/6/3 11:59
 */
public class BinarySearchArray {

    public static void main(String[] args) {
        int[] array = {1, 3, 4, 6, 7, 8, 10, 12, 42};
        System.out.println(search(array, 11));
    }

    public static int search(int[] array, int n) {
        int leftIndex = 0;
        int rightIndex = array.length - 1;
        while (leftIndex <= rightIndex) {
            int middleIndex = (leftIndex + rightIndex) >> 1;
            if (array[middleIndex] == n) {
                return middleIndex;
            } else if (array[middleIndex] < n) {
                leftIndex = middleIndex + 1;
            } else {
                rightIndex = middleIndex - 1;
            }
        }
        return -1;

    }
}
